package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.ApplyToProgUpdateDTO;
import org.jsola.hr.dto.ApplyToProgAddDTO;
import org.jsola.hr.dto.ApplyToProgEmpeeInfoDTO;
import org.jsola.hr.query.ApplyToProgQuery;
import org.jsola.hr.service.IApplyToProgService;
import org.jsola.hr.vo.ApplyToProgListVO;
import org.jsola.hr.vo.ApplyToProgVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrApplyToProgController")
@RequestMapping(HrConstants.API_PATH + "/v1/applyToProgs")
public class ApplyToProgController implements IApplyToProgControllerAPI {

    @Autowired
    private IApplyToProgService applyToProgService;

    @Override
    @PostMapping
    public Result<ApplyToProgVO> save(@Valid @RequestBody ApplyToProgAddDTO applyToProgAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增方案适用设置权限校验

        return Result.success(
                applyToProgService.save(applyToProgAddDTO, tokenUser)
        );
    }


    @Override
    @PostMapping(value = "/add")
    public Result<Object> add(@Valid @RequestBody ApplyToProgAddDTO applyToProgAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增方案适用设置权限校验
        Object object = applyToProgService.add(applyToProgAddDTO, tokenUser);
        Result result=   Result.success(object);
      if (object instanceof  List){
          List list= (List) object;
          if (list.size()>0){
              if (list.get(0).getClass() ==ApplyToProgVO.class){
                  result.setMsg("方案所选项包含在其它方案中");
              }else {
                  result.setMsg("适用员工重复");
              }
          }

        }
            return result;
    }

    @Override
    @PutMapping
    public Result<Object> update(@Valid @RequestBody ApplyToProgUpdateDTO applyToProgUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改方案适用设置权限校验

        Object object = applyToProgService.update(applyToProgUpdateDTO, tokenUser);
      Result result=  Result.success(object);
         if (object instanceof  List){
            List list= (List) object;
            if (list.size()>0){
                if (list.get(0).getClass() ==ApplyToProgVO.class){
                    result.setMsg("方案所选项包含在其它方案中");
                }else {
                    result.setMsg("适用员工重复");
                }
            }

        }
        return result;
    }



    @Override
    @DeleteMapping(value = "/{applyToProgId}")
    public Result<Integer> delete(@PathVariable String applyToProgId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除方案适用设置权限校验

        int count = applyToProgService.deleteByIds(tokenUser, applyToProgId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] applyToProgIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除方案适用设置权限校验

        int count = applyToProgService.deleteByIds(tokenUser, applyToProgIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{applyToProgId}")
    public Result<ApplyToProgVO> findById(@PathVariable String applyToProgId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看方案适用设置权限校验

        return Result.success(
            applyToProgService.selectById(applyToProgId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/findByBusProgId/{busProgId}")
    public Result<ApplyToProgVO> findByBusProgId(@PathVariable String busProgId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看方案适用设置权限校验
        return  Result.success(
                applyToProgService.selectByBusProgId(busProgId,tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ApplyToProgListVO>> listAll(@Valid ApplyToProgQuery applyToProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看方案适用设置权限校验

        return Result.success(
            applyToProgService.select(applyToProgQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ApplyToProgListVO>> page(@Valid ApplyToProgQuery applyToProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            applyToProgService.selectPage(applyToProgQuery, tokenUser.getSiteId())
        );
    }
    @Override
    @GetMapping(value = "/getApplyToProgEmployee")
    public Result<List<ApplyToProgEmpeeInfoDTO>> getApplyToProgEmployee(String busProgType, String companyId){
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            applyToProgService.getApplyToProgEmployee(busProgType,tokenUser.getSiteId(), companyId)
        );
    }

}

